{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n",
      "F:\\anaconda\\anaconda\\envs\\tensorflow\\lib\\site-packages\\sklearn\\utils\\fixes.py:313: FutureWarning: numpy not_equal will not check object identity in the future. The comparison did not return the same result as suggested by the identity (`is`)) and will change.\n",
      "  _nan_object_mask = _nan_object_array != _nan_object_array\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from keras.models import load_model\n",
    "import keras.backend as K\n",
    "from sklearn.model_selection import KFold\n",
    "from keras.layers import Input, Dense, Conv1D, Flatten, MaxPooling1D, Conv2D, MaxPooling2D, AveragePooling2D, Dropout, Reshape, normalization\n",
    "#from keras.models import Model\n",
    "from keras.utils import to_categorical\n",
    "from keras.layers.recurrent import LSTM\n",
    "from sklearn import metrics\n",
    "import random\n",
    "from keras.models import model_from_json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0.12206   ]\n",
      "  [-0.162339  ]\n",
      "  [-0.0600364 ]\n",
      "  ..., \n",
      "  [ 0.0553366 ]\n",
      "  [-0.100643  ]\n",
      "  [ 0.0914919 ]]\n",
      "\n",
      " [[-0.00554577]\n",
      "  [ 0.187162  ]\n",
      "  [-0.105255  ]\n",
      "  ..., \n",
      "  [-0.0919779 ]\n",
      "  [-0.0929276 ]\n",
      "  [ 0.052444  ]]\n",
      "\n",
      " [[ 0.244697  ]\n",
      "  [ 0.293217  ]\n",
      "  [-0.0971591 ]\n",
      "  ..., \n",
      "  [-0.0711738 ]\n",
      "  [-0.0810454 ]\n",
      "  [ 0.0470242 ]]\n",
      "\n",
      " ..., \n",
      " [[ 0.358097  ]\n",
      "  [ 0.0905413 ]\n",
      "  [ 0.0331105 ]\n",
      "  ..., \n",
      "  [ 0.0624149 ]\n",
      "  [ 0.0660773 ]\n",
      "  [-0.0471668 ]]\n",
      "\n",
      " [[ 0.50615   ]\n",
      "  [ 0.328114  ]\n",
      "  [-0.0362345 ]\n",
      "  ..., \n",
      "  [-0.00577319]\n",
      "  [ 0.107257  ]\n",
      "  [-0.10438   ]]\n",
      "\n",
      " [[ 0.278387  ]\n",
      "  [ 0.150123  ]\n",
      "  [ 0.00274092]\n",
      "  ..., \n",
      "  [-0.115661  ]\n",
      "  [ 0.135467  ]\n",
      "  [-0.118267  ]]]\n",
      "X.shape:  (20, 200, 1)\n",
      "Y.shape:  (20,)\n",
      "20/20 [==============================] - 0s 1ms/step\n",
      "accuracy [0.19218169152736664, 0.69999998807907104, 0.6428571343421936, 0.89999997615814209, 0.74999988079071045, 9.0, 1.0, 5.0, 5.0]\n",
      "[[ 0.90504688]\n",
      " [ 0.85448664]\n",
      " [ 0.81821752]\n",
      " [ 0.82772362]\n",
      " [ 0.49430528]\n",
      " [ 0.98519313]\n",
      " [ 0.79041398]\n",
      " [ 0.80507296]\n",
      " [ 0.66225749]\n",
      " [ 0.77847576]\n",
      " [ 0.56336623]\n",
      " [ 0.50522602]\n",
      " [ 0.49430528]\n",
      " [ 0.60802239]\n",
      " [ 0.49430528]\n",
      " [ 0.77098638]\n",
      " [ 0.70090854]\n",
      " [ 0.49430528]\n",
      " [ 0.49430528]\n",
      " [ 0.49430528]]\n",
      "[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n"
     ]
    }
   ],
   "source": [
    "def precision(y_true, y_pred):\n",
    "    # Calculates the precision\n",
    "    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n",
    "    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n",
    "    precision = true_positives / (predicted_positives + K.epsilon())\n",
    "    return precision\n",
    "\n",
    "def recall(y_true, y_pred):\n",
    "    # Calculates the recall\n",
    "    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n",
    "    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n",
    "    recall = true_positives / (possible_positives + K.epsilon())\n",
    "    return recall\n",
    "\n",
    "def f1(test_Y, pre_test_y):\n",
    "    \"\"\"F1-score\"\"\"\n",
    "    Precision = precision(test_Y, pre_test_y)\n",
    "    Recall = recall(test_Y, pre_test_y)\n",
    "    f1 = 2 * ((Precision * Recall) / (Precision + Recall + K.epsilon()))\n",
    "    return f1 \n",
    "\n",
    "def TP(test_Y,pre_test_y):\n",
    "    TP = K.sum(K.round(K.clip(test_Y * pre_test_y, 0, 1)))#TP\n",
    "    return TP\n",
    "\n",
    "def FN(test_Y,pre_test_y):\n",
    "    TP = K.sum(K.round(K.clip(test_Y * pre_test_y, 0, 1)))#TP\n",
    "    P=K.sum(K.round(K.clip(test_Y, 0, 1)))\n",
    "    FN = P-TP #FN=P-TP\n",
    "    return FN\n",
    "\n",
    "def TN(test_Y,pre_test_y):\n",
    "    TN=K.sum(K.round(K.clip((test_Y-K.ones_like(test_Y))*(pre_test_y-K.ones_like(pre_test_y)), 0, 1)))#TN\n",
    "    return TN\n",
    "\n",
    "def FP(test_Y,pre_test_y):\n",
    "    N = (-1)*K.sum(K.round(K.clip(test_Y-K.ones_like(test_Y), -1, 0)))#N\n",
    "    TN=K.sum(K.round(K.clip((test_Y-K.ones_like(test_Y))*(pre_test_y-K.ones_like(pre_test_y)), 0, 1)))#TN\n",
    "    FP=N-TN\n",
    "    return FP\n",
    "\n",
    "\n",
    "data = np.array(pd.read_csv(\"3_test_vecs.csv\"))\n",
    "pos_number = 10 # NOTE: the number of postive sample in test file\n",
    "    \n",
    "X1 = data[0:pos_number, 1:]\n",
    "Y1 = data[0:pos_number, 0]\n",
    "X2 = data[pos_number:, 1:]\n",
    "Y2 = data[pos_number:, 0]\n",
    "X = np.concatenate([X1, X2], 0)\n",
    "Y = np.concatenate([Y1, Y2], 0)\n",
    "#Y = Y.reshape((Y.shape[0], -1))\n",
    "X = np.expand_dims(X, 2)\n",
    "print (X)\n",
    "print (\"X.shape: \", X.shape)\n",
    "print (\"Y.shape: \", Y.shape)\n",
    "\n",
    "model_name = 'CNN_model.h5'\n",
    "model_back = load_model(model_name,\n",
    "                        custom_objects={'precision': precision,'recall':recall,'f1':f1,'TP':TP,'FN':FN,'TN':TN,'FP':FP})\n",
    "# model = load_model('pcsf.h5')\n",
    "accuracy = model_back.evaluate(X,Y)\n",
    "# print 'loss', loss\n",
    "print ('accuracy', accuracy)\n",
    "maxprobability = model_back.predict(X)\n",
    "np.set_printoptions(threshold=np.inf)\n",
    "print (maxprobability)\n",
    "predictclass = model_back.predict(X)\n",
    "predictclass = np.argmax(predictclass,axis=1)\n",
    "print (predictclass)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
